﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PingYou
{
    class Common
    {
        #region public static bool IsMatchMail(string paramMail) 检测邮件格式是否正确
        /// <summary>
        /// 检测邮件格式是否正确
        /// </summary>
        /// <param name="paramMail"></param>
        /// <returns></returns>
        public static bool IsMatchMail(string paramMail)
        {
            bool result = true;
            try
            {
                result = Regex.IsMatch(paramMail, @"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$");
                //  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
            }
            catch (Exception myEx)
            {
                WriteLogErr(myEx.Message);
            }
            return result;
        }
        #endregion

        #region public static void WriteLogErr(string myErrEx) 系统错误日志
        /// <summary>
        /// 系统错误日志
        /// </summary>
        /// <param name="myEx"></param>
        public static void WriteLogErr(string myErrEx)
        {
            //depth所请求的堆栈帧的索引，在获得所调用的方法0，获得此方法的方法1，获得调用此方法的方法的方法2。。。类推
            //MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
            try
            {
                StackTrace st = new StackTrace();
                string methodName = st.GetFrame(1).GetMethod().Name;
                string className = st.GetFrame(1).GetMethod().DeclaringType.ToString();

                FileStream fs = new FileStream(AppDomain.CurrentDomain.BaseDirectory + @"LogErr.txt", FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter m_streamWriter = new StreamWriter(fs);
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                m_streamWriter.WriteLine("");
                m_streamWriter.WriteLine("类名：" + className + "; 方法：" + methodName + "; 时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss[.fff] ") + " 错误信息：" + myErrEx);
                m_streamWriter.Flush();
                m_streamWriter.Close();
                fs.Close();
            }
            catch
            {
                
            }
        }
        #endregion
    }
}
